#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Author: Lee
# File: public.py
# Time: 2022/9/23 10:26
import os
import xlrd3
import yaml
from util.log import logger
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from config.setting import *


def locate(driver, type, express):
    '''
    显示等待元素加载，并进行点击
    :param driver:
    :param type:
    :param express:
    :return:
    '''
    type = type.lower()
    global element
    webdriver_wait = WebDriverWait(driver, LOCATE_TIME, POLL_FREQUENCY)
    if type == 'id':
        element = webdriver_wait.until(lambda driver: driver.find_element(By.ID, express))
    elif type == 'name':
        element = webdriver_wait.until(lambda driver: driver.find_element(By.NAME, express))
    elif type == 'class':
        element = webdriver_wait.until(lambda driver: driver.find_element(By.CLASS_NAME, express))
    elif type == 'tag':
        element = webdriver_wait.until(lambda driver: driver.find_element(By.TAG_NAME, express))
    elif type == 'link':
        element = webdriver_wait.until(lambda driver: driver.find_element(By.LINK_TEXT, express))
    elif type == 'plink':
        element = webdriver_wait.until(lambda driver: driver.find_element(By.PARTIAL_LINK_TEXT, express))
    elif type == 'xpath':
        element = webdriver_wait.until(lambda driver: driver.find_element(By.XPATH, express))
    elif type == 'css':
        element = webdriver_wait.until(lambda driver: driver.find_element(By.CSS_SELECTOR, express))
    return element


def use_and_click(driver, type, express):
    '''
    结合locate方法，定位元素并点击
    :param driver:
    :param type:
    :param express:
    :return:
    '''
    file_name = os.path.join(TEST_DATA_PATH, 'stander_data.yaml')
    data_list = read_yaml(file_name)
    element = locate(driver, type, express)
    assert_result(data_list[0].get('expect'), element.text, '==')
    element.click()
    logger.info(f'点击元素：{element.text}')


def use_and_sendkeys(driver, type, express, keys):
    '''
    结合locate方法定位元素，并输入数据
    :param driver:
    :param type:
    :param express:
    :param keys:
    :return:
    '''
    locate(driver, type, express).send_keys(keys)


def read_excel(file_name, sheet_name):
    '''
    从excel读取测试数据
    :param file_name:
    :param sheet_name:
    :return: 读取到的数据
    '''
    # 根据excel文件名，获取表格中的所有sheet页
    workbook = xlrd3.open_workbook(file_name)
    # 根据sheet页的名字获取对应sheet页中的数据
    data = workbook.sheet_by_name(sheet_name)

    # 获取数据总行数
    rows = data.nrows
    list_data = []
    # 循环获取每一行的值，并添加到list_data列表中去
    for i in range(1, rows):
        list_data.append(data.row_values(i))
    return list_data


def read_yaml(file_name):
    '''
    从yaml文件中读取测试数据
    :param file_name:
    :return:
    '''
    # 打开文件流，读取数据
    with open(file_name, encoding='utf-8') as file:
        yaml_data = yaml.load(file, Loader=yaml.FullLoader)
        file.close()
    return yaml_data


def assert_result(expect, actual, assert_type):
    '''
    断言方法
    :param expect:
    :param actual:
    :param assert_type:
    :return: 断言结果
    '''
    try:
        if assert_type == '==':
            assert actual == expect
        elif assert_type == 'in':
            assert actual in expect
        elif assert_type == 'notin':
            assert actual not in expect
        elif assert_type == '!=':
            assert actual != expect
    except AssertionError:
        logger.error(f'预期结果是：{expect}，但是得到的实际结果是：{actual}，断言失败！')
        return False
    else:
        logger.info(f'预期结果是：{expect}，得到的实际结果是：{actual}，断言成功！')
        return True


if __name__ == '__main__':
    assert_result(1, 2, '==')